<?php

class Utilitarios_PapeisController extends Zend_Rest_Controller {

	public function init() {
		/* Initialize action controller here */
		$ajaxContext = $this->_helper->contextSwitch();
		$ajaxContext->setAutoJsonSerialization(true);
		$ajaxContext
		->addActionContext('index', array('json'))
		->addActionContext('get', array('json'))
		->addActionContext('post', array('json'))
		->addActionContext('put', array('json'))
		->addActionContext('delete', array('json'))
		->initContext('json');

		if($this->getRequest()->getParam('tree')!='')
			$this->{$this->getRequest()->getParam('tree').'Tree'}();
		if($this->getRequest()->getParam('grid')!='')
			$this->{$this->getRequest()->getParam('grid').'Grid'}();
	}

	public function usuarios(){
		//$modelUsuariosPorPapel=new Application_Model_Utilitarios_UsuariosPorPapel();
		//$id = $this->getRequest()->getParam('id');

		$model_usuario=new Application_Model_Utilitarios_Usuario();
		$usuarios=$model_usuario->index();
		print Zend_Json::encode($usuarios);
		exit;
	}
	
	public function usuariosTree(){
		$modelUsuariosPorPapel=new Application_Model_Utilitarios_UsuarioPorPapel();
		$id = $this->getRequest()->getParam('id');
	
		$node=$this->getRequest()->getParam('node');
		$filter=array(array('property'=>'primario','value'=>$node));
	
		$model_usuario=new Application_Model_Utilitarios_Usuario();
		$usuarios=$model_usuario->index($filter);
	
		if($node!=""){
			foreach($usuarios['data'] as &$a0){
				$a0["expanded"]=true;
				$a0["checked"]=false;
				if($id){
					$usuariosPorPapel=$modelUsuariosPorPapel->index(array(
							array('property'=>'usuario_id','value'=>$a0['id']),
							array('property'=>'papel_id','value'=>$id),
					));
					if($usuariosPorPapel['total']>0)$a0["checked"]=true;
				}
				if(isset($a0['icone']))$a0["icon"]=$a0['icone'];
				$a0['data']=array();
			}
		}
		print Zend_Json::encode($usuarios);
		exit;
	}
	
	
	public function funcoesTree(){
		$modelFuncoesPorPapel=new Application_Model_Utilitarios_FuncaoPorPapel();
		$id = $this->getRequest()->getParam('id');

		$node=$this->getRequest()->getParam('node');
		$filter=array(array('property'=>'primario','value'=>$node));

		$model_funcao=new Application_Model_Utilitarios_Funcao();
		$funcao=$model_funcao->index($filter);

		if($node!=""){
			foreach($funcao['data'] as &$a0){
				$a0["expanded"]=false;
				$a0["checked"]=false;
				if($id){
					$funcoesPorPapel=$modelFuncoesPorPapel->index(array(
							array('property'=>'funcao_id','value'=>$a0['id']),
							array('property'=>'papel_id','value'=>$id),
					));
					if($funcoesPorPapel['total']>0)$a0["checked"]=true;
				}
				if(isset($a0['icone']))$a0["icon"]=$a0['icone'];
			}
		}
		print Zend_Json::encode($funcao);
		exit;
	}

	public function indexAction(){
		$model=new Application_Model_Utilitarios_Papel();
		$filter=Zend_Json::decode($this->getRequest()->getParam('filter'));
		$result=$model->index($filter, $this->getRequest()->getParam('start'), $this->getRequest()->getParam('limit'), $this->getRequest()->getParam('sort'));
		print Zend_Json::encode($result);
		exit;
	}

	public function getAction(){
		$id = $this->getRequest()->getParam('id');
		$model=new Application_Model_Utilitarios_Papel();
		$result=$model->get($id);
		print Zend_Json::encode($result);
		exit;
	}

	public function postAction(){
		$post = Zend_Json::decode($this->getRequest()->getRawBody());
		$model=new Application_Model_Utilitarios_Papel();
		$result=$model->post($post);
		$funcoes=Zend_Json::decode($this->getRequest()->getParam('funcoes'));
		if($this->getRequest()->getParam('funcoes')!=''){
			$funcoes=Zend_Json::decode($this->getRequest()->getParam('funcoes'));
			$model=new Application_Model_Utilitarios_FuncaoPorPapel();
			foreach($funcoes as $funcao){
				$row=$model->post(array('papel_id'=>$result['data']['id'],'funcao_id'=>$funcao));
			}
		}
		if($this->getRequest()->getParam('usuarios')!=''){
			$usuarios=Zend_Json::decode($this->getRequest()->getParam('usuarios'));
			$model=new Application_Model_Utilitarios_UsuarioPorPapel();
			foreach($usuarios as $usuario){
				$row=$model->post(array('papel_id'=>$result['data']['id'],'usuario_id'=>$usuario));
			}
		}
		print Zend_Json::encode($result);
		exit;
	}

	public function putAction(){
		$id = $this->getRequest()->getParam('id');
		$post = Zend_Json::decode($this->getRequest()->getRawBody());
		$model=new Application_Model_Utilitarios_Papel();
		$result=$model->put($id,$post);
		if($this->getRequest()->getParam('funcoes')!=''){
			$funcoes=Zend_Json::decode($this->getRequest()->getParam('funcoes'));
			$model=new Application_Model_Utilitarios_FuncaoPorPapel();
			$funcoesXpapel=$model->index(array(array('property'=>'papel_id','value'=>$id)));
			foreach($funcoesXpapel['data'] as $funcao){
				$model->delete($funcao['id']);
			}
			foreach($funcoes as $funcao){
				$row=$model->post(array('papel_id'=>$id,'funcao_id'=>$funcao));
			}
		}
		if($this->getRequest()->getParam('usuarios')!=''){
			$usuarios=Zend_Json::decode($this->getRequest()->getParam('usuarios'));
			$model=new Application_Model_Utilitarios_UsuarioPorPapel();
			$usuariosXpapel=$model->index(array(array('property'=>'papel_id','value'=>$id)));
			foreach($usuariosXpapel['data'] as $usuario){
				$model->delete($usuario['id']);
			}
			foreach($usuarios as $usuario){
				$row=$model->post(array('papel_id'=>$id,'usuario_id'=>$usuario));
			}
		}
		print Zend_Json::encode($result);
		exit;
	}

	public function deleteAction(){
		$id = $this->getRequest()->getParam('id');
		$model=new Application_Model_Utilitarios_Papel();
		$result=$model->delete($id);
		$model=new Application_Model_Utilitarios_FuncaoPorPapel();
		$funcoesXpapel=$model->index(array(array('property'=>'papel_id','value'=>$id)));
		foreach($funcoesXpapel['data'] as $funcao){
			$model->delete($funcao['id']);
		}
		$model=new Application_Model_Utilitarios_UsuarioPorPapel();
		$usuariosXpapel=$model->index(array(array('property'=>'papel_id','value'=>$id)));
		foreach($usuariosXpapel['data'] as $usuario){
			$model->delete($usuario['id']);
		}
		print Zend_Json::encode($result);
		exit;
	}

}